package com.zyd.login.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zyd.common.AjaxUtils;
import com.zyd.common.CommonController;
import com.zyd.xtgl.domain.vo.SysModule;
import com.zyd.xtgl.domain.vo.TbAdmUser;
import com.zyd.xtgl.logic.IAdmUserFacade;

@Controller
@RequestMapping("/login")
public class LoginAction extends CommonController {
	private IAdmUserFacade admUserImpl;

	@Resource(name = "admUserImpl")
	public void setAdmUserImpl(IAdmUserFacade admUserImpl) {
		this.admUserImpl = admUserImpl;
	}

	/**
	 * 冗余方法（ajax实例）将List转换为json数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getList")
	public String getList(HttpServletRequest request,
			HttpServletResponse response) {
		return AjaxUtils.ajaxList(request, response, null);
	}

	/**
	 * 登陆
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=login")
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) {
		if (request.getSession().getAttribute("user") != null) {
			return new ModelAndView("welcome/mainpage");
		} else {
			String userName = request.getParameter("txtUser").trim();
			String password = request.getParameter("txtPass").trim();
			boolean isSuccess = admUserImpl.checkUser(userName, password);
			if (isSuccess) {
				TbAdmUser user = admUserImpl.getCuruserinfo(userName);
				request.getSession().setAttribute("user", user);
				return new ModelAndView("welcome/mainpage");
			} else {
				return new ModelAndView("login");
			}
		}
	}

	/**
	 * 根据用户取得用户权限 返回JSON数据，页面上由js解析easyui展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getMenu")
	public String getMenu(HttpServletRequest request,
			HttpServletResponse response) {
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		List<SysModule> menu = admUserImpl.getMainMenu(user.getUserid());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("menus", menu);
		return AjaxUtils.ajaxList(request, response, map);
	}

	/**
	 * 验证用户名
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws java.lang.Exception
	 */
	@RequestMapping(params = "method=checkUsername")
	public ModelAndView checkUsername(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		String returnStr = "";
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
			// 验证用户名
			boolean isSuccess = admUserImpl.checkUserName(username);
			if (isSuccess) {
				returnStr="用户可用~~";
			} else {
				returnStr="用户名不存在~~";
			}
		out.write(returnStr);
		out.close();
		return null;
	}

	@RequestMapping(params = "method=logout")
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) {
		request.getSession().removeAttribute("user");
		request.getSession().removeAttribute("menu");
		return new ModelAndView("login");
	}
}
